﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Common
{
    public class ZyEncrypter
    {
        public static byte[] GetMd5(string str)
        {

            //string cl = str;
            //string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            //for (int i = 0; i < s.Length; i++)
            //{
            //    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符

            //    pwd = pwd + s[i].ToString("x2");

            //}
            return s;
        }//获得MD5值 BYTE
        public static string GetMd5str(string str)
        {

            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符

                pwd = pwd + s[i].ToString("x2");

            }
            return pwd.ToUpper();
        }//获得MD5值

        public static string StringTobase64(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            string base64 = Convert.ToBase64String(buffer);
            return base64;
        }
        public static string Base64ToString(string str)
        {
            byte[] buffer = Convert.FromBase64String(str);
            string rstr = Encoding.UTF8.GetString(buffer);
            return str;
        }
        public static string DesEncript(string strContent, string strKey)
        {
            if (string.IsNullOrEmpty(strContent))
            {
                return "";
            }
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            DES.Key = hashMD5.ComputeHash(Encoding.UTF8.GetBytes(strKey));
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESEncrypt = DES.CreateEncryptor();
            byte[] Buffer = Encoding.UTF8.GetBytes(strContent);
            string rstr = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            rstr = rstr.Replace('=', '!');
            rstr = rstr.Replace('+', '@');
            rstr = rstr.Replace('/', '|');
            return rstr;
        }
        public static string DesDecript(string strContent, string strKey)
        {
            if (string.IsNullOrEmpty(strContent))
            {
                return "";
            }
            string instr = strContent.Replace('@', '+');
            instr = instr.Replace('|', '/');
            instr = instr.Replace('!', '=');
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

            DES.Key = hashMD5.ComputeHash(Encoding.UTF8.GetBytes(strKey));
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESEncrypt = DES.CreateDecryptor();
            try
            {
                byte[] Buffer = Convert.FromBase64String(instr);
                return Encoding.UTF8.GetString(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
